package com.woniu.product.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.woniu.model.Result;
import com.woniu.product.dto.ClassDto;
import com.woniu.product.dto.VenueAdminDto;
import com.woniu.product.dto.VenueClassDto;
import com.woniu.product.form.*;
import com.woniu.product.form.venueClass.LoadVenueClassByConditionForm;
import com.woniu.product.form.venueClass.LoadVenueClassForm;
import com.woniu.product.model.Class;
import com.woniu.product.param.*;
import com.woniu.product.param.venueClass.LoadVenueClassParam;
import com.woniu.product.service.ClassService;
import com.woniu.util.JwtUtil;
import com.woniu.util.RedisUtil;
import com.woniu.util.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.swing.*;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年07月12日
 */
@RestController
@RequestMapping("/class")
@Api(tags = "团课/私教接口")
public class ClassController {

    @Resource
    private ClassService classService;
    @Autowired
    private RedisUtil redisUtil;
    @GetMapping("/ByIdClass")
    @ApiOperation("通过团课Id查一个团课")
    public Result<?> ByIdClass(Integer id)throws Exception{
        Class aClass = classService.ByIdClass(id);
        return Result.getResult(Class.class).setData(aClass).setMsg("查询成功").setCode(200);
    }
    @PostMapping("/addClass")
    @ApiOperation("新增团课")
    public Result<?> addClass(@RequestBody AddClassForm form)throws Exception{
        String token = WebUtil.getToken();
        String refreshToken = redisUtil.get(token);
        VenueAdminDto venueAdminDto = JSONUtil.toBean(JSONUtil.toJsonStr(JwtUtil.getVenueAdmin(refreshToken)),VenueAdminDto.class);
        AddClassParam param = BeanUtil.toBean(form, AddClassParam.class);
        classService.addClass(param);
        return Result.getResult(null).setMsg("新增成功").setCode(200);
    }

    /**
     * 新增私教课
     * @param form
     * @param token
     * @return
     * @throws Exception
     */
    @PostMapping("/addPersonal")
    @ApiOperation("新增私教课")
    public Result<?> addPersonal(AddPersonalForm form, @RequestHeader String token)throws Exception{
        String refreshToken = redisUtil.get(token);
//        Integer venueId = Integer.parseInt(JwtUtil.parseVenueAdminToken(refreshToken).get("venueId").toString());
//        Object venueAdminDto = JwtUtil.getVenueAdmin(refreshToken);

        VenueAdminDto venueAdminDto = JSONUtil.toBean(JSONUtil.toJsonStr(JwtUtil.getVenueAdmin(refreshToken)),VenueAdminDto.class);

        AddPersonalParam param = BeanUtil.toBean(form, AddPersonalParam.class);
        classService.addPersonal(param);
        return Result.getResult(null).setMsg("新增私教课成功").setCode(200);

    }

    /**
     *  通过教练id查询该教练的团课
     * @param coachId 教练id
     * @return
     * @throws Exception
     */
    @GetMapping("/coachByIdClass")
    @ApiOperation("通过教练id查询该教练的团课")
    public Result<?> coachByIdClass(Integer coachId)throws Exception{
        List list = classService.coachByIdClass(coachId);
        return Result.getResult(List.class).setData(list).setMsg("查询成功").setCode(200);

    }
    @ApiOperation("通过教练id查询该教练的私教课")
    @GetMapping("/coachByIdPersonal")
    public Result<?> coachByIdPersonal(Integer coachId)throws Exception{
        List list = classService.coachByIdPersonal(coachId);
         return Result.getResult(List.class).setData(list).setMsg("查询成功").setCode(200);
    }
    @GetMapping("/venueByIdClass")
    @ApiOperation("通过场馆id查询该场馆的团课")
    public Result<?> venueByIdClass(Integer venueId)throws Exception{
        List list = classService.venueByIdClass(venueId);
        return Result.getResult(List.class).setData(list).setMsg("查询成功").setCode(200);
    }

    @PostMapping("/loadVenueClassByCondition")
    @ApiOperation("通过场馆id查询该场馆的团课")
    public Result loadVenueClassByCondition(@RequestBody LoadVenueClassByConditionForm form)throws Exception{
        List<VenueClassDto> list = classService.loadVenueClassByCondition(form);
        return Result.getResult().setData(list).setMsg("查询成功");
    }


    @PostMapping("/delPersonal")
    @ApiOperation("删除私教课")
    public Result<?> delPersonal(Integer classId)throws Exception{
        if (classId==null){
            return Result.getResult(null).setMsg("参数异常").setCode(500);
        }
        classService.delPersonal(classId);
        return Result.getResult(null).setMsg("删除私教课成功").setCode(200);
    }

    @PostMapping("/delClass")
    @ApiOperation("删除团课")
    public Result<?> delClass(Integer classId)throws Exception{
        if (classId==null){
            return Result.getResult(null).setMsg("参数异常").setCode(500);
        }

        classService.delClass(classId);
        return Result.getResult(null).setMsg("删除团课成功").setCode(200);
    }


    @PostMapping("/putawayClass")
    @ApiOperation("团课上架")
    public Result<?> putawayClass(Integer classId,@RequestHeader String token)throws Exception{
        String refreshToken = redisUtil.get(token);
//      Integer venueId = Integer.parseInt(JwtUtil.parseVenueAdminToken(refreshToken).get("venueId").toString());
//        VenueAdminDto venueAdminDto = (VenueAdminDto)JwtUtil.getVenueAdmin(refreshToken);
        VenueAdminDto venueAdminDto = JSONUtil.toBean(JSONUtil.toJsonStr(JwtUtil.getVenueAdmin(refreshToken)),VenueAdminDto.class);

        classService.putawayClass(classId,venueAdminDto.getVenueId());
        return Result.getResult().setMsg("团课上架成功");
    }

    /**
     *
     * @param className 团课名
     * @param classType 课程类型 1团课 2私教
     * @return
     * @throws Exception
     */
    @PostMapping("/loadAllClass")
    @ApiOperation("通过名称加载所有团课对象")
    public Result<?> loadAllClass(String className,Integer classType)throws Exception{
        List<Class> list = classService.loadAllClass(className,classType);
        return Result.getListResult(Class.class).setData(list).setMsg("查询所有团课成功");
    }

    /**
     * 查询团课集合
     * @return
     */
    @GetMapping("/loadListClass")
    @ApiOperation("查询团课集合")
    public Result<?>loadListClass()throws Exception{
        List list = classService.loadListClass();
        return Result.getListResult(List.class).setCode(200).setMsg("查询成功").setData(list);
    }
    @GetMapping("/loadListPersonal")
    @ApiOperation("查询私教集合")
    public Result loadListPersonal()throws Exception{
        List list = classService.loadListPersonal();
        return Result.getListResult(List.class).setCode(200).setMsg("查询成功").setData(list);
    }



    /**
     *  修改团课状态为完课
     * @param form
     * @return
     * @throws Exception
     */
    @PostMapping("/updateClassState")
    @ApiOperation("修改团课状态为完课")
    public Result<?> updateClassState(UpdateClassStateForm form)throws Exception{
       if (form.getClassId()==null||form.getClassState()==null||form.getClassType()==null){
           return Result.getResult(null).setMsg("数据异常").setCode(500);
       }
        classService.updateClassState(BeanUtil.toBean(form, UpdateClassStateParam.class));
           return Result.getResult(null).setMsg("修改状态成功").setCode(200);
    }
    @PostMapping("/修改私教课状态为可预约")
    public Result<?> updateClassPersonal(UpdateClassPersonalForm form)throws Exception{
        if (form.getClassId()==null||form.getClassState()==null||form.getClassType()==null){
            return Result.getResult(null).setMsg("数据异常").setCode(500);
        }
        classService.updateClassPersonal(BeanUtil.toBean(form,UpdateClassPersonalParam.class));
        return Result.getResult(null).setMsg("修改私教课状态成功").setCode(200);
    }




    /**
     *  教练端分页查询所有团课
     * @param form
     * @param token
     * @return
     * @throws Exception
     */

    @GetMapping("/pageAllClass")
    @ApiOperation("教练端分页查询所有团课")
    public Result pageAllClass(PageCoachClassForm form, @RequestHeader String token)throws Exception{
        String refreshToken = redisUtil.get(token);
//        Integer coachId = Integer.parseInt(JwtUtil.parseVenueAdminToken(refreshToken).get("id").toString());
        VenueAdminDto venueAdminDto = JSONUtil.toBean(JSONUtil.toJsonStr(JwtUtil.getVenueAdmin(refreshToken)),VenueAdminDto.class);

        PageCoachClassParam param = BeanUtil.toBean(form, PageCoachClassParam.class);
        param.setCoachId(venueAdminDto.getCoachId());
        PageInfo<ClassDto> classDtoPage = classService.pageAllClass(param);
        return Result.getResult().setData(classDtoPage).setMsg("分页查询成功").setCode(200);
    }
    @GetMapping("/pageAllVenueClass")
    @ApiOperation("场馆分页查询该场馆的团课")
    public Result pageAllVenueClass(PageAllVenueClassForm form,@RequestHeader String token)throws Exception{
        String refreshToken = redisUtil.get(token);
        VenueAdminDto venueAdminDto = JSONUtil.toBean(JSONUtil.toJsonStr(JwtUtil.getVenueAdmin(refreshToken)),VenueAdminDto.class);
        PageAllVenueClassParam param = BeanUtil.toBean(form, PageAllVenueClassParam.class);
        param.setVenueId(venueAdminDto.getVenueId());
        PageInfo<VenueClassDto> pageInfo = classService.pageAllVenueClass(param);
        return Result.getResult().setData(pageInfo).setMsg("分页查询成功").setCode(200);
    }

    @PostMapping("/load")
    @ApiOperation("场馆分页查询该场馆的团课")
    public Result load(@RequestBody LoadVenueClassForm form)throws Exception{
        LoadVenueClassParam param = BeanUtil.toBean(form, LoadVenueClassParam.class);
        PageInfo<VenueClassDto> pageInfo = classService.load(param);
        return Result.getResult().setData(pageInfo).setMsg("成功");
    }


    @PostMapping("/loadClassByIds")
    public Result loadClassByIds(@RequestBody Set<Integer> ids) {
        return Result.getResult().setMsg("成功").setData(classService.listByIds(ids));
    }


}

